import axios from 'axios'
import router from '../router/index.js'
import {Message} from 'element-ui';

const uuidv1 = require('uuid/v1');

var httpUtils = {
    header: {'Content-Type': 'application/json;charset=utf-8', "X-Access-Token": "", "timestamp": '', "nonce": ""},//header信息
    base: process.env.VUE_APP_BASE_API,
    post: function (url, param) {
        httpUtils.header['X-Access-Token'] = localStorage.getItem("X-Access-Token") == null ? "" : localStorage.getItem("X-Access-Token");
        httpUtils.header.timestamp = new Date().getTime();
        httpUtils.header.nonce = uuidv1();//随机值
        let tUrl = httpUtils.base + url;
        return new Promise((resolve) => {
            axios.post(tUrl + '?t=' + Math.random(), param, {headers: httpUtils.header}).then(res => {
                var code = res.data.code;
                if (code == 401) {//登录失效
                    localStorage.clear()
                    if ((undefined == goLogin || '' == goLogin || null == goLogin) && goLogin != false) {
                        Message.error('登录失效，请重新登录')
                        // setTimeout(() => {
                        //     // router.push({name: 'Login', query: {back: true}})
                        //     this.$router.push({path: '/login', query: {back: true}})
                        // }, 1000)
                    }
                    resolve(res.data)
                    return false
                } else if (code == 404) {//404
                    router.push({name: '404'})
                    resolve(res.data)
                    return false
                }
                resolve(res.data)
            })
        })
    },
    get: function (url, param, goLogin, forResponse) {
        httpUtils.header['X-Access-Token'] = localStorage.getItem("X-Access-Token") == null ? "" : localStorage.getItem("X-Access-Token");
        httpUtils.header.timestamp = new Date().getTime();
        httpUtils.header.nonce = uuidv1();//随机值
        if (forResponse) {
            httpUtils.header.responseType = 'arraybuffer'
        }
        let tUrl = httpUtils.base + url;
        return new Promise((resolve) => {
            let req = {params: param, headers: httpUtils.header}
            if (forResponse) {
                req.responseType = 'blob'
            }
            axios.get(tUrl + '?t=' + Math.random(), req).then(res => {
                var code = res.data.code;
                if (code == 401) {//登录失效
                    localStorage.clear()
                    if ((undefined == goLogin || '' == goLogin || null == goLogin) && goLogin != false) {
                        // Message.error('登录失效，请重新登录')
                        // setTimeout(() => {
                        //     // router.push({name: 'Login', query: {back: true}})
                        //     this.$router.push({path: '/login', query: {back: true}})
                        // }, 1000)
                    }
                    resolve(res.data)
                    return false
                } else if (code == 404) {//404
                    router.push({name: '404'})
                    resolve(res.data)
                    return false
                }

                if (forResponse) {
                    resolve(res)
                } else {
                    resolve(res.data)
                }
            })
        })
    }
}

export default httpUtils
